package com.luo.gmall.pms.service.impl;

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.luo.gmall.pms.entity.AttrEntity;
import com.luo.gmall.pms.entity.SkuAttrValueEntity;
import com.luo.gmall.pms.entity.SpuAttrValueEntity;
import com.luo.gmall.pms.mapper.AttrMapper;
import com.luo.gmall.pms.mapper.SkuAttrValueMapper;
import com.luo.gmall.pms.mapper.SpuAttrValueMapper;
import com.luo.gmall.pms.service.AttrService;
import com.luo.gmall.pms.vo.AttrValueVo;
import com.luo.gmall.pms.vo.ItemGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luo.gmall.common.bean.PageResultVo;
import com.luo.gmall.common.bean.PageParamVo;

import com.luo.gmall.pms.mapper.AttrGroupMapper;
import com.luo.gmall.pms.entity.AttrGroupEntity;
import com.luo.gmall.pms.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrMapper attrMapper;

    @Autowired
    private SkuAttrValueMapper skuAttrValueMapper;

    @Autowired
    private SpuAttrValueMapper spuAttrValueMapper;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<AttrGroupEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<AttrGroupEntity> getAttrByCatId(Long categoryId) {
        //1、根据类别id查询出属性分组列表
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);

        List<AttrGroupEntity> attrGroupEntities = baseMapper.selectList(queryWrapper);
        // 判断是否为空集合
        if (CollectionUtils.isEmpty(attrGroupEntities)) {
            return attrGroupEntities;
        }
        //2、根据每个分组列表id查询出对应的属性列表
        for (AttrGroupEntity attrGroupEntity : attrGroupEntities) {
            QueryWrapper<AttrEntity> queryWrappers = new QueryWrapper<>();
            queryWrappers.eq("group_id", attrGroupEntity.getId());
            // 过滤销售属性
            queryWrappers.eq("type", 1);
            //将查询到的数据设置到该列表上
            attrGroupEntity.setAttrEntities(attrMapper.selectList(queryWrappers));
        }
        return attrGroupEntities;
    }

    @Override
    public List<ItemGroupVo> queryWithItemGroupByCategoryIdAndSpuIdAndSkuId(Long categoryId, Long spuId, Long skuId) {
        // 根据categoryId查询出对应的属性分组id
        List<AttrGroupEntity> attrGroupEntities = baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("category_id", categoryId));
        if (CollectionUtils.isEmpty(attrGroupEntities)) {
            return null;
        }
        // 遍历attrGroup下的id
        List<ItemGroupVo> itemGroupVos = new ArrayList<>();
        attrGroupEntities.forEach(attrGroupEntity -> {

            // 根据groupId查询出对应的attr_id
            List<AttrEntity> attrEntities = attrMapper.selectList(new QueryWrapper<AttrEntity>().eq("group_id", attrGroupEntity.getId()));
            // 判空
            if (!CollectionUtils.isEmpty(attrEntities)) {
                ItemGroupVo itemGroupVo = new ItemGroupVo();
                itemGroupVo.setId(attrGroupEntity.getId());
                itemGroupVo.setName(attrGroupEntity.getName());
                List<Long> attrIds = attrEntities.stream().map(AttrEntity::getId).collect(Collectors.toList());
                // 根据attrIds，skuId查询出对应SkuAttrValueEntities
                List<SkuAttrValueEntity> skuAttrValueEntities = skuAttrValueMapper.selectList(new QueryWrapper<SkuAttrValueEntity>().eq("sku_id", skuId).in("attr_id", attrIds));
                List<AttrValueVo> attrValueVoList = new ArrayList<>();
                // 判空
                if (!CollectionUtils.isEmpty(skuAttrValueEntities)) {
                    attrValueVoList.addAll(skuAttrValueEntities.stream().map(skuAttrValueEntity -> {
                        AttrValueVo attrValueVo = new AttrValueVo();
                        // 属性值copy
                        BeanUtils.copyProperties(skuAttrValueEntity, attrValueVo);
                        return attrValueVo;
                    }).collect(Collectors.toList()));
                }
                // 根据attrIds，spuId查询出对应SpuAttrValueEntities
                List<SpuAttrValueEntity> spuAttrValueEntities = spuAttrValueMapper.selectList(new QueryWrapper<SpuAttrValueEntity>().eq("spu_id", spuId).in("attr_id", attrIds));
                // 判空
                if (!CollectionUtils.isEmpty(spuAttrValueEntities)) {
                    attrValueVoList.addAll(spuAttrValueEntities.stream().map(spuAttrValueEntity -> {
                        AttrValueVo attrValueVo = new AttrValueVo();
                        // 属性值copy
                        BeanUtils.copyProperties(spuAttrValueEntity, attrValueVo);
                        return attrValueVo;
                    }).collect(Collectors.toList()));
                }
                // 设置attrs
                itemGroupVo.setAttrs(attrValueVoList);
                // 添加一个数据
                itemGroupVos.add(itemGroupVo);
            }

        });
        return itemGroupVos;
    }


}